package com.wx.rabbitmq.producer;

import com.wx.rabbitmq.config.MqConfig;
import com.wx.rabbitmq.entity.Order;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @ClassName RabbitSend
 * @Author wx
 * @Description 发送消息对象
 * @Date 2018-11-25-21:24
 */
@Component
public class RabbitSend {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private MqConfig mqConfig;

    /**
     * @author: wx
     * @description: 回调函数: confirm确认
     * @param null
     * @date: 2018/11/25
     * @return:
     */
    final RabbitTemplate.ConfirmCallback confirmCallback = new RabbitTemplate.ConfirmCallback() {
        @Override
        public void confirm(CorrelationData correlationData, boolean ack, String cause) {
            System.err.println("correlationData: " + correlationData);
            System.err.println("ack: " + ack);
            if(!ack){
                System.err.println("异常处理....");
            }
        }
    };

    /**
     * @author: wx
     * @description: 回调函数: return返回
     * @param null
     * @date: 2018/11/25
     * @return:
     */
    final RabbitTemplate.ReturnCallback returnCallback = new RabbitTemplate.ReturnCallback() {
        @Override
        public void returnedMessage(org.springframework.amqp.core.Message message, int replyCode, String replyText,
                                    String exchange, String routingKey) {
            System.err.println("return exchange: " + exchange + ", routingKey: "
                    + routingKey + ", replyCode: " + replyCode + ", replyText: " + replyText);
        }
    };
    


    /**
     * @methodName: send
     * @author: wx
     * @description: 发送消息方法
     * @param message
     * @param properties
     * @date: 2018/11/25
     * @return: void
     */
    public void send(Object message, Map<String, Object> properties) throws Exception {
        setCallback();
        MessageHeaders mhs = new MessageHeaders(properties);
        Message msg = MessageBuilder.createMessage(message, mhs);
        //全局唯一
        CorrelationData correlationData = new CorrelationData(String.valueOf(System.currentTimeMillis()));
        rabbitTemplate.convertAndSend(mqConfig.getTestExchangeName(), mqConfig.getTestRoutingKey(), msg, correlationData);
    }

    /**
     * @methodName: sendOrder
     * @author: wx
     * @description: 发送消息方法重载
     * @param order
     * @date: 2018/11/25
     * @return: void
     */
    public void sendOrder(Order order) throws Exception {
        setCallback();
        //id 全局唯一
        CorrelationData correlationData = new CorrelationData(String.valueOf(System.currentTimeMillis()));
        rabbitTemplate.convertAndSend(mqConfig.getTestExchangeName_2(), mqConfig.getTestRoutingKey(), order, correlationData);
    }
    
    /**
     * @methodName: setCallback
     * @author: wx
     * @description: 设置回调方法
     * @param
     * @date: 2018/11/25
     * @return: void
     */
    private  void   setCallback(){
        //设置消息确认机制回调函数
        rabbitTemplate.setConfirmCallback(confirmCallback);
        //设置不可路由消息回调函数
        rabbitTemplate.setReturnCallback(returnCallback);
    }

}
